<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *   文件调查。
 *
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zsquestion extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://dxshowxya.for.pub/index.php?s=/api/basemanage/Yuyuemanage/getList
     */

     public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
         $param=" and w.question_is_del=0";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id";
        }
        if (!empty($keyword)) {
            $param.=" and (w.question_content like '%$keyword%' or w.question_content like '%$keyword%') ";
        }
        $sql2="select w.*,c.id as exhibition_id,c.title ";
        $sql2.=" from crm_zs_question_wenda as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
        $sql2.=" order by w.question_category,w.question_sort ";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        //var_dump($sql2);die();
        $list=Db::query($sql2);
        $sql21=" select count(w.id) as count1 ";
        $sql21.=" from crm_zs_question_wenda as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $com =new CommonPlus();
        $data['question_insert_date']=time();
        $data['exhibition_id']=$this->request->param('exhibition_id');   // 1单选 2多选3  填空
        $data['question_type']=$this->request->param('question_type');   // 1单选 2多选3  填空
        $data['question_sort']=$this->request->param('question_sort');   // 排序
        $data['question_category']=$this->request->param('question_category');  // 试题分类 ，比如10题都是xx调查，然后呢我，
        $data['question_content']=$this->request->param('question_content'); // 标题
        $data['question_bs_type']=$this->request->param('question_bs_type'); // 标题
        $data['question_purchase_type']=$this->request->param('question_purchase_type'); // 标题
        $data['question_flag']=$this->request->param('question_flag')?$this->request->param('question_flag'):0;// 两组答案 二选一
        $data['qustion_en']=$this->request->param('qustion_en')?$this->request->param('qustion_en'):0;// 中英文
        $data['question_content_sel']=$this->request->param('question_content_sel'); // 答案选项
        $res=$com->insert_com_dan($data, '', 'zs_question_wenda');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('zs_question_wenda')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('zs_question_wenda')->where(['id'=>$id])->update(['question_is_del'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['question_type']=$this->request->param('question_type');   // 1单选 2多选3  填空 4：多选择
        $data['question_category']=$this->request->param('question_category');  // 试题分类 ，比如10题都是xx调查，然后呢我，
        $data['question_content']=$this->request->param('question_content'); // 标题
        $data['question_flag']=$this->request->param('question_flag')?$this->request->param('question_flag'):0;// 两组答案 二选一
        $data['qustion_en']=$this->request->param('qustion_en')?$this->request->param('qustion_en'):0;// 两组答案 二选一
        $data['question_content_sel']=$this->request->param('question_content_sel'); // 选项  @ 如果多选择 拼接
        $data['question_bs_type']=$this->request->param('question_bs_type'); // 标题
        $data['question_purchase_type']=$this->request->param('question_purchase_type'); // 标题
        $data['question_sort']=$this->request->param('question_sort'); // 选项  @ 如果多选择 拼接
        $data['question_update_date']=time();
        $where['id'] = $id;
        $tbname="zs_question_wenda";
        $r=$com->edit_com_fxlist($data, $where, $tbname);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function getDetail()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="zs_question_wenda";
        $r=$com->sel_com_one('222', $where, $tbname);
        $r['question_content_sel']=unserialize($r['question_content_sel']);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }





    // the end


}
